using System.Windows.Input;
using Bookvar.ServiceModel.Actions;
using Bookvar.UI.Commands;

namespace Bookvar.UI.ViewModel
{
    public partial class AttachmentsWindowViewModel
    {
        private class SaveAttachmentCommand : CommandModel
        {
            private readonly AttachmentsWindowViewModel viewModel;

            public SaveAttachmentCommand(AttachmentsWindowViewModel viewModel)
                : base( BookvarCommands.SaveAttachment )
            {
                this.viewModel = viewModel;
            }

            protected internal override void OnExecute( object sender, ExecutedRoutedEventArgs e )
            {
                string fileName = e.Parameter as string;

                if ( !string.IsNullOrEmpty( fileName ) )
                {
                    var action = new SaveFileResourceToFileAction(
                        PersistenceService, viewModel.Mindmap, viewModel.SelectedAttachment, fileName);

                    ActionService.Execute( action );
                }
            }

            protected internal override void OnCanExecute( object sender, CanExecuteRoutedEventArgs e )
            {
                e.CanExecute = viewModel.SelectedAttachment != null;
                e.Handled = true;
            }
        }
    }
}